#include <QToolBar>
#include <QAction>
#include <QMovie>
#include <QLabel>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QDebug>
#include <QProcess>
#include <QDir>

#include "displayitem.h"
#include "qrwindow.h"
#include "../utils.h"

namespace Haha
{
class DisplayItemPrivate
{
public:
    DisplayItemPrivate(DisplayItem* q);
    DisplayItem* q_ptr;
    QUrl url; // local url
    QUrl remoteUrl; // remote url
    QAction* scanAct = nullptr;
    QAction* locateAct = nullptr;
    QLabel* label = nullptr;
    QToolBar* bar = nullptr;
    QSize size;

    void createUi();
    void layoutWidgets();
};

static const int BarHeight = 30;
static const QSize MinSize = QSize(150, 150);

static inline bool operator<(const QSize& lhs, const QSize& rhs)
{
    return lhs.width() < rhs.width() || lhs.height() < rhs.height();
}

DisplayItemPrivate::DisplayItemPrivate(DisplayItem *q) : q_ptr(q)
{

}

void DisplayItemPrivate::createUi()
{
    bar = new QToolBar(q_ptr);
    bar->setStyleSheet("background-color:gray;");
    label = new QLabel(q_ptr);

    if (Jcr::Utils::isGif(url)) {
        QMovie* movie = new QMovie(q_ptr);
        label->setMovie(movie);
        movie->setFileName(url.toString());
        movie->start();
        size = movie->frameRect().size();
    } else {
        QPixmap pixmap(url.toString());
        label->setPixmap(pixmap);
        label->setFixedSize(pixmap.size());
        size = pixmap.size();
    }

    if (size < MinSize)
        size = MinSize;

    q_ptr->setMinimumSize(QSize(size.width(), size.height() + BarHeight));
    q_ptr->setWindowTitle(url.fileName());
    layoutWidgets();

    scanAct = new QAction(QIcon(":/images/qrcode.png"), QObject::tr("Scan QR Code"), q_ptr);
    locateAct = new QAction(QIcon(":/images/explorer.png"), QObject::tr("Locate"), q_ptr);

    bar->addAction(scanAct);
    bar->addAction(locateAct);

    q_ptr->connect(scanAct, SIGNAL(triggered()), q_ptr, SLOT(scan()));
    q_ptr->connect(locateAct, SIGNAL(triggered()), q_ptr, SLOT(locate()));

    label->show();
}

void DisplayItemPrivate::layoutWidgets()
{
    if (!label)
        return;

    bar->move(QPoint(0, 0));
    bar->resize(this->size.width(), BarHeight);
    label->move(QPoint(0, BarHeight));
    label->resize(size.width(), size.height());
    q_ptr->setFixedSize(this->size);
}

DisplayItem::DisplayItem(const QUrl& url, QWidget *parent) : QWidget(parent)
{
    d_ptr = new DisplayItemPrivate(this);
    d_ptr->url = url;
    d_ptr->createUi();
}

DisplayItem::~DisplayItem()
{
    delete d_ptr;
}

QUrl DisplayItem::url() const
{
    return d_ptr->url;
}

void DisplayItem::setRemoteUrl(const QUrl& url)
{
    d_ptr->remoteUrl = url;
}

QUrl DisplayItem::remoteUrl() const
{
    return d_ptr->remoteUrl;
}


void DisplayItem::scan()
{
    Haha::QRWindow* qrw = Haha::QRWindow::instance();
    qrw->setUrl(remoteUrl());
    qrw->show();
}

void DisplayItem::locate()
{
#ifdef Q_OS_WIN
    QString strCmd = QString("explorer /select,%1").arg(QDir::toNativeSeparators(d_ptr->url.toString()));
    QProcess::startDetached(strCmd);
#endif
}

QSize DisplayItem::sizeHint() const
{
    return QSize(200, 200);
}

void DisplayItem::resizeEvent(QResizeEvent *e)
{
    d_ptr->layoutWidgets();
    QWidget::resizeEvent(e);
}

} // namespace haha
